<!DOCTYPE html>

<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

<title>class Table - RDoc Documentation</title>

<link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
</script>

<script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>


<body id="top" class="class">
<nav id="metadata">
  <nav id="home-section" class="section">
  <h3 class="section-header">
    <a href="./index.html">Home</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </h3>
</nav>


  <nav id="search-section" class="section project-section" class="initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <h3 class="section-header">
      <input type="text" name="search" placeholder="Search" id="search-field"
             title="Type to search, Up and Down to navigate, Enter to load">
    </h3>
  </form>

  <ul id="search-results" class="initially-hidden"></ul>
</nav>


  

  <div id="file-metadata">
    <nav id="file-list-section" class="section">
  <h3 class="section-header">Defined In</h3>
  <ul>
    <li>lib/tablestakes.rb
  </ul>
</nav>

    
  </div>

  <div id="class-metadata">
    
    <nav id="parent-class-section" class="section">
  <h3 class="section-header">Parent</h3>
  
  <p class="link">Object
  
</nav>

    <!-- Included Modules -->
<nav id="includes-section" class="section">
  <h3 class="section-header">Included Modules</h3>

  <ul class="link-list">
  
  
    <li><span class="include">Enumerable</span>
  
  
  </ul>
</nav>

    
    <!-- Method Quickref -->
<nav id="method-list-section" class="section">
  <h3 class="section-header">Methods</h3>

  <ul class="link-list">
    
    <li ><a href="#method-c-new">::new</a>
    
    <li ><a href="#method-i-add_column">#add_column</a>
    
    <li ><a href="#method-i-add_row">#add_row</a>
    
    <li ><a href="#method-i-add_rows">#add_rows</a>
    
    <li ><a href="#method-i-bottom">#bottom</a>
    
    <li ><a href="#method-i-column">#column</a>
    
    <li ><a href="#method-i-count">#count</a>
    
    <li ><a href="#method-i-del_column">#del_column</a>
    
    <li ><a href="#method-i-del_row">#del_row</a>
    
    <li ><a href="#method-i-each">#each</a>
    
    <li ><a href="#method-i-empty-3F">#empty?</a>
    
    <li ><a href="#method-i-get_columns">#get_columns</a>
    
    <li ><a href="#method-i-get_rows">#get_rows</a>
    
    <li ><a href="#method-i-intersect">#intersect</a>
    
    <li ><a href="#method-i-join">#join</a>
    
    <li ><a href="#method-i-length">#length</a>
    
    <li ><a href="#method-i-row">#row</a>
    
    <li ><a href="#method-i-select">#select</a>
    
    <li ><a href="#method-i-size">#size</a>
    
    <li ><a href="#method-i-sort">#sort</a>
    
    <li ><a href="#method-i-sort-21">#sort!</a>
    
    <li ><a href="#method-i-sub">#sub</a>
    
    <li ><a href="#method-i-sub-21">#sub!</a>
    
    <li ><a href="#method-i-tally">#tally</a>
    
    <li ><a href="#method-i-to_a">#to_a</a>
    
    <li ><a href="#method-i-to_s">#to_s</a>
    
    <li ><a href="#method-i-top">#top</a>
    
    <li ><a href="#method-i-union">#union</a>
    
    <li ><a href="#method-i-where">#where</a>
    
    <li ><a href="#method-i-write_file">#write_file</a>
    
  </ul>
</nav>

  </div>

  <div id="project-metadata">
    
    <nav id="classindex-section" class="section project-section">
  <h3 class="section-header">Class and Module Index</h3>

  <ul class="link-list">
  
    <li><a href="./OrderedRow.html">OrderedRow</a>
  
    <li><a href="./Table.html">Table</a>
  
  </ul>
</nav>

  </div>
</nav>

<div id="documentation">
  <h1 class="class">class Table</h1>

  <div id="description" class="description">
    
<p>This class is a Ruby representation of a table. All data is captured as
type <code>String</code> by default. Columns are referred to by their
<code>String</code> headers which are assumed to be identified in the first
row of the input file. Output is written by default to tab-delimited files
with the first row serving as the header names.</p>

  </div><!-- description -->

  
  
  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    

    
    <!-- Attributes -->
    <section id="attribute-method-details" class="method-section section">
      <h3 class="section-header">Attributes</h3>

      
      <div id="attribute-i-headers" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">headers</span><span
            class="attribute-access-type">[R]</span>
        </div>

        <div class="method-description">
        
        <p>The headers attribute contains the table headers used to reference columns
in the <code>Table</code>.  All headers are represented as
<code>String</code> types.</p>
        
        </div>
      </div>
      
    </section><!-- attribute-method-details -->
    

    <!-- Methods -->
    
     <section id="public-class-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Class Methods</h3>

    
      <div id="method-c-new" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">new</span><span
            class="method-args">(input=nil)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Instantiate a <code>Table</code> object using a tab-delimited file</p>

<h4 id="method-c-new-label-Attributes">Attributes<span><a href="#method-c-new-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>input</code>
<dd>
<p>OPTIONAL <code>Array</code> of rows or <code>String</code> to identify the
name of the tab-delimited file to read</p>
</dd></dl>

<h4 id="method-c-new-label-Examples">Examples<span><a href="#method-c-new-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities = Table.new() # empty table
cities = Table.new([ [&quot;City&quot;, &quot;State], [&quot;New York&quot;, &quot;NY&quot;], [&quot;Dallas&quot;, &quot;TX&quot;] ]) # create from Array of rows
cities = Table.new(&quot;cities.txt&quot;) # read from file
cities = Table.new(capitals)  # create from table</pre>
          
          

          
          <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 43</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">input</span>=<span class="ruby-keyword">nil</span>)
  <span class="ruby-ivar">@headers</span> = []
  <span class="ruby-ivar">@table</span> = {}
  <span class="ruby-ivar">@indices</span> = {}
  
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">input</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:fetch</span>)
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">input</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:fetch</span>)
      <span class="ruby-comment">#create Table from rows</span>
      <span class="ruby-identifier">add_rows</span>(<span class="ruby-identifier">input</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">input</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:upcase</span>)
    <span class="ruby-comment"># a string, then read_file</span>
    <span class="ruby-identifier">read_file</span>(<span class="ruby-identifier">input</span>)
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">input</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:headers</span>)
    <span class="ruby-identifier">input</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">add_row</span>(<span class="ruby-identifier">row</span>) }
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># else create empty +Table+</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- new-source -->
          
        </div>

        

        
      </div><!-- new-method -->

    
    </section><!-- public-class-method-details -->
  
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Instance Methods</h3>

    
      <div id="method-i-add_column" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">add_column</span><span
            class="method-args">(*args)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Add a column to the <a href="Table.html">Table</a>. Raises ArgumentError if
the column name is already taken  or there are not the correct number of
values.</p>

<h4 id="method-i-add_column-label-Attributes">Attributes<span><a href="#method-i-add_column-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>args</code>
<dd>
<p>Array of <code>String</code> to identify the name of the column (see
examples)</p>
</dd></dl>

<h4 id="method-i-add_column-label-Examples">Examples<span><a href="#method-i-add_column-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.add_column(&quot;City&quot;, [&quot;New York&quot;, &quot;Dallas&quot;, &quot;San Franscisco&quot;])
cities.add_column([&quot;City&quot;,&quot;New York&quot;, &quot;Dallas&quot;, &quot;San Franscisco&quot;])
cities.add_column(&quot;City&quot;, &quot;New York&quot;, &quot;Dallas&quot;, &quot;San Franscisco&quot;)</pre>
          
          

          
          <div class="method-source-code" id="add_column-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 114</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">add_column</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Array</span>
    <span class="ruby-identifier">args</span>.<span class="ruby-identifier">flatten!</span>
    <span class="ruby-identifier">colname</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
    <span class="ruby-identifier">column_vals</span> = <span class="ruby-identifier">args</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid Arguments to add_column&quot;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Duplicate Column Name!&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-keyword">unless</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">empty?</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">column_vals</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">length</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Number of elements in column does not match existing table&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">append_col</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">column_vals</span>)    
<span class="ruby-keyword">end</span></pre>
          </div><!-- add_column-source -->
          
        </div>

        

        
      </div><!-- add_column-method -->

    
      <div id="method-i-add_row" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">add_row</span><span
            class="method-args">(*row)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Add a row to the <a href="Table.html">Table</a>, appending it to the end.
Raises ArgumentError if  there are not the correct number of values.</p>

<h4 id="method-i-add_row-label-Attributes">Attributes<span><a href="#method-i-add_row-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>row</code>
<dd>
<p><code>Array</code> to hold the row values</p>
</dd></dl>

<h4 id="method-i-add_row-label-Examples">Examples<span><a href="#method-i-add_row-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities = Table.new.add_row( [&quot;City&quot;, &quot;State&quot;] ) # create new Table with headers
cities.add_row(&quot;New York&quot;, &quot;NY&quot;) # add data row to Table</pre>
          
          

          
          <div class="method-source-code" id="add_row-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 158</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">add_row</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">row</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Array</span>
    <span class="ruby-identifier">row</span> = <span class="ruby-identifier">row</span>.<span class="ruby-identifier">flatten</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid Arguments to add_row&quot;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">empty?</span>
      <span class="ruby-ivar">@headers</span> = <span class="ruby-identifier">row</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">unless</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">length</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Wrong number of fields in Table input&quot;</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">append_row</span>(<span class="ruby-identifier">row</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- add_row-source -->
          
        </div>

        

        
      </div><!-- add_row-method -->

    
      <div id="method-i-add_rows" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">add_rows</span><span
            class="method-args">(array_of_rows)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Add one or more rows to the <a href="Table.html">Table</a>, appending it to
the end. Raises ArgumentError if  there are not the correct number of
values.  The first row becomes the table headers if currently undefined.</p>

<h4 id="method-i-add_rows-label-Attributes">Attributes<span><a href="#method-i-add_rows-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>array_of_rows</code>
<dd>
<p><code>Array</code> of <code>Arrays</code> to hold the rows values</p>
</dd></dl>

<h4 id="method-i-add_rows-label-Examples">Examples<span><a href="#method-i-add_rows-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.add_rows([ [&quot;New York&quot;, &quot;NY&quot;], [&quot;Austin&quot;, &quot;TX&quot;] ])</pre>
          
          

          
          <div class="method-source-code" id="add_rows-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 141</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">add_rows</span>(<span class="ruby-identifier">array_of_rows</span>)
  <span class="ruby-identifier">array_of_rows</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">add_row</span>(<span class="ruby-identifier">r</span>.<span class="ruby-identifier">clone</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- add_rows-source -->
          
        </div>

        

        
      </div><!-- add_rows-method -->

    
      <div id="method-i-bottom" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">bottom</span><span
            class="method-args">(colname, num=1)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Returns counts of the least frequent values found in a given column in the
form of a <a href="Table.html">Table</a>.  Raises ArgumentError if the
column is not found.  If no limit is given to the number of values, only
the least frequent value will be returned.</p>

<h4 id="method-i-bottom-label-Attributes">Attributes<span><a href="#method-i-bottom-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to count</p>
</dd><dt><code>num</code>
<dd>
<p>OPTIONAL <code>String</code> number of values to return</p>
</dd></dl>

<h4 id="method-i-bottom-label-Examples">Examples<span><a href="#method-i-bottom-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.bottom(&quot;State&quot;)  # returns a Table with the least frequent state in the cities Table
cities.bottom(&quot;State&quot;, 10)  # returns a Table with the 10 least frequent states in the cities Table</pre>
          
          

          
          <div class="method-source-code" id="bottom-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 318</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">bottom</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">num</span>=<span class="ruby-value">1</span>)
  <span class="ruby-identifier">freq</span> = <span class="ruby-identifier">tally</span>(<span class="ruby-identifier">colname</span>).<span class="ruby-identifier">to_a</span>[<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>].<span class="ruby-identifier">sort_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">v</span> }
  <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">freq</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">num</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>].<span class="ruby-identifier">unshift</span>([<span class="ruby-identifier">colname</span>,<span class="ruby-string">&quot;Count&quot;</span>]))
<span class="ruby-keyword">end</span></pre>
          </div><!-- bottom-source -->
          
        </div>

        

        
      </div><!-- bottom-method -->

    
      <div id="method-i-column" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">column</span><span
            class="method-args">(colname)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Return a copy of a column from the table, identified by column name.
Returns empty Array if column name not found.</p>

<h4 id="method-i-column-label-Attributes">Attributes<span><a href="#method-i-column-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the name of the column</p>
</dd></dl>
          
          

          
          <div class="method-source-code" id="column-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 80</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">column</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-comment"># return empty Array if column name not found</span>
  <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>) 
    <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>()
  <span class="ruby-keyword">else</span>
    <span class="ruby-constant">Array</span>(<span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>])
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- column-source -->
          
        </div>

        

        
      </div><!-- column-method -->

    
      <div id="method-i-count" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">count</span><span
            class="method-args">(colname=nil, value=nil)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Counts the number of instances of a particular string, given a column name,
and returns an integer &gt;= 0. Returns <code>nil</code> if the column is
not found. If no parameters are given, returns the number of rows in the
table.</p>

<h4 id="method-i-count-label-Attributes">Attributes<span><a href="#method-i-count-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p>OPTIONAL <code>String</code> to identify the column to count</p>
</dd><dt><code>value</code>
<dd>
<p>OPTIONAL <code>String</code> value to count</p>
</dd></dl>

<h4 id="method-i-count-label-Examples">Examples<span><a href="#method-i-count-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.count  # returns number of rows in cities Table
cities.size   # same as cities.count
cities.length # same as cities.count
cities.count(&quot;State&quot;, &quot;NY&quot;)  # returns the number of rows with State == &quot;NY&quot;</pre>
          
          

          
          <div class="method-source-code" id="count-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 264</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">count</span>(<span class="ruby-identifier">colname</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">value</span>=<span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">colname</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span>
    <span class="ruby-keyword">if</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
      <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">each_key</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-keyword">return</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">e</span>).<span class="ruby-identifier">length</span> }
    <span class="ruby-keyword">else</span>
      <span class="ruby-keyword">return</span> <span class="ruby-value">0</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">colname</span>)
  
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>]
    <span class="ruby-identifier">result</span> = <span class="ruby-value">0</span>
    <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">val</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span> 
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">result</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">nil</span> 
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- count-source -->
          
        </div>

        
        <div class="aliases">
          Also aliased as: <a href="Table.html#method-i-size">size</a>, <a href="Table.html#method-i-length">length</a>
        </div>
        

        
      </div><!-- count-method -->

    
      <div id="method-i-del_column" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">del_column</span><span
            class="method-args">(colname)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Delete a column from the <a href="Table.html">Table</a>. Raises
ArgumentError if the column name does not exist.</p>

<h4 id="method-i-del_column-label-Attributes">Attributes<span><a href="#method-i-del_column-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the name of the column</p>
</dd></dl>

<h4 id="method-i-del_column-label-Examples">Examples<span><a href="#method-i-del_column-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.del_column(&quot;State&quot;) # returns table without &quot;State&quot; column</pre>
          
          

          
          <div class="method-source-code" id="del_column-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 182</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">del_column</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Column name does not exist!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
  
  <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- del_column-source -->
          
        </div>

        

        
      </div><!-- del_column-method -->

    
      <div id="method-i-del_row" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">del_row</span><span
            class="method-args">(rownum)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Delete a row from the <a href="Table.html">Table</a>. Raises ArgumentError
if the row number is not found</p>

<h4 id="method-i-del_row-label-Attributes">Attributes<span><a href="#method-i-del_row-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>rownum</code>
<dd>
<p><code>FixNum</code> to hold the row number</p>
</dd></dl>

<h4 id="method-i-del_row-label-Examples">Examples<span><a href="#method-i-del_row-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.del_row(3)  # deletes row with index 3 (4th row)
cities.del_row(-1) # deletes last row (per Ruby convention)</pre>
          
          

          
          <div class="method-source-code" id="del_row-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 200</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">del_row</span>(<span class="ruby-identifier">rownum</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">rownum</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">length</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Row number does not exist!&quot;</span> 
  <span class="ruby-keyword">end</span>
  <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
    <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>].<span class="ruby-identifier">delete_at</span>(<span class="ruby-identifier">rownum</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- del_row-source -->
          
        </div>

        

        
      </div><!-- del_row-method -->

    
      <div id="method-i-each" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">each</span><span
            class="method-args">() { |nextrow| ... }</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Defines an iterator for <code>Table</code> which produces rows of data
(headers omitted) for its calling block.</p>
          
          

          
          <div class="method-source-code" id="each-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 65</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">each</span>
  <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">nextrow</span> = []
    <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">nextrow</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>].<span class="ruby-identifier">clone</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-identifier">nextrow</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- each-source -->
          
        </div>

        

        
      </div><!-- each-method -->

    
      <div id="method-i-empty-3F" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">empty?</span><span
            class="method-args">()</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Return true if the <a href="Table.html">Table</a> is empty, false
otherwise.</p>
          
          

          
          <div class="method-source-code" id="empty-3F-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 100</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">empty?</span>
  <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- empty-3F-source -->
          
        </div>

        

        
      </div><!-- empty-3F-method -->

    
      <div id="method-i-get_columns" class="method-detail method-alias">
        
        <div class="method-heading">
          <span class="method-name">get_columns</span><span
            class="method-args">(*columns)</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
        </div>

        

        
        <div class="aliases">
          Alias for: <a href="Table.html#method-i-select">select</a>
        </div>
        
      </div><!-- get_columns-method -->

    
      <div id="method-i-get_rows" class="method-detail method-alias">
        
        <div class="method-heading">
          <span class="method-name">get_rows</span><span
            class="method-args">(colname, condition=nil)</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
        </div>

        

        
        <div class="aliases">
          Alias for: <a href="Table.html#method-i-where">where</a>
        </div>
        
      </div><!-- get_rows-method -->

    
      <div id="method-i-intersect" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">intersect</span><span
            class="method-args">(table2, colname, col2name=colname)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Return an Array with the intersection of columns from different tables,
eliminating duplicates. Return nil if a column is not found.</p>

<h4 id="method-i-intersect-label-Attributes">Attributes<span><a href="#method-i-intersect-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>table2</code>
<dd>
<p><code>Table</code> to identify the secondary table in the intersection</p>
</dd><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to intersection</p>
</dd><dt><code>col2name</code>
<dd>
<p>OPTIONAL <code>String</code> to identify the column in the second table to
intersection</p>
</dd></dl>

<h4 id="method-i-intersect-label-Examples">Examples<span><a href="#method-i-intersect-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.intersect(capitals, &quot;City&quot;, &quot;Capital&quot;)  # returns Array with all capitals that are also in the cities table</pre>
          
          

          
          <div class="method-source-code" id="intersect-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 513</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">intersect</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-identifier">colname</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid table!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Table</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)

  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">colname</span>) <span class="ruby-operator">&amp;</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">col2name</span>)
<span class="ruby-keyword">end</span></pre>
          </div><!-- intersect-source -->
          
        </div>

        

        
      </div><!-- intersect-method -->

    
      <div id="method-i-join" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">join</span><span
            class="method-args">(table2, colname, col2name=colname)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Given a second table to join against, and a field/column, return a
<code>Table</code> which contains a join of the two tables. Join only lists
the common column once, under the column name of the first table (if
different from the name of thee second). All columns from both tables are
returned. Returns <code>nil</code> if the column is not found.</p>

<h4 id="method-i-join-label-Attributes">Attributes<span><a href="#method-i-join-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>table2</code>
<dd>
<p><code>Table</code> to identify the secondary table in the join</p>
</dd><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to join on</p>
</dd><dt><code>col2name</code>
<dd>
<p>OPTIONAL <code>String</code> to identify the column in the second table to
join on</p>
</dd></dl>

<h4 id="method-i-join-label-Examples">Examples<span><a href="#method-i-join-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.join(capitals, &quot;City&quot;, &quot;Capital&quot;)  # returns a Table of cities that are also state capitals
capitals.join(cities, &quot;State&quot;)  # returns a Table of capital cities with populations info from the cities table</pre>
          
          

          
          <div class="method-source-code" id="join-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 425</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">join</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-identifier">colname</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid table!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Table</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)
  <span class="ruby-identifier">t2_col_index</span> = <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">col2name</span>)
  
  <span class="ruby-comment"># ensure no duplication of header values</span>
  <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">header</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">header</span>)
      <span class="ruby-identifier">update_header</span>(<span class="ruby-identifier">header</span>, <span class="ruby-string">&#39;_&#39;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">header</span> )
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">header</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">colname</span>
        <span class="ruby-identifier">colname</span> = <span class="ruby-string">&#39;_&#39;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">colname</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">result</span> = [ <span class="ruby-constant">Array</span>(<span class="ruby-ivar">@headers</span>) <span class="ruby-operator">+</span> <span class="ruby-constant">Array</span>(<span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>) ]
  <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">t2_index</span> = <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">col2name</span>).<span class="ruby-identifier">find_index</span>(<span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>][<span class="ruby-identifier">index</span>])
    <span class="ruby-keyword">unless</span> <span class="ruby-identifier">t2_index</span>.<span class="ruby-identifier">nil?</span>
      <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">row</span>(<span class="ruby-identifier">index</span>) <span class="ruby-operator">+</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">row</span>(<span class="ruby-identifier">t2_index</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">result</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span> <span class="ruby-comment">#no rows selected</span>
    <span class="ruby-keyword">return</span> <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>) 
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- join-source -->
          
        </div>

        

        
      </div><!-- join-method -->

    
      <div id="method-i-length" class="method-detail method-alias">
        
        <div class="method-heading">
          <span class="method-name">length</span><span
            class="method-args">(colname=nil, value=nil)</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
        </div>

        

        
        <div class="aliases">
          Alias for: <a href="Table.html#method-i-count">count</a>
        </div>
        
      </div><!-- length-method -->

    
      <div id="method-i-row" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">row</span><span
            class="method-args">(index)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Return a copy of a row from the table as an <code>Array</code>, given an
index (i.e. row number). Returns empty Array if the index is out of bounds.</p>

<h4 id="method-i-row-label-Attributes">Attributes<span><a href="#method-i-row-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>index</code>
<dd>
<p><code>FixNum</code> indicating index of the row.</p>
</dd></dl>
          
          

          
          <div class="method-source-code" id="row-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 94</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">row</span>(<span class="ruby-identifier">index</span>)    
  <span class="ruby-constant">Array</span>(<span class="ruby-identifier">get_row</span>(<span class="ruby-identifier">index</span>))
<span class="ruby-keyword">end</span></pre>
          </div><!-- row-source -->
          
        </div>

        

        
      </div><!-- row-method -->

    
      <div id="method-i-select" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">select</span><span
            class="method-args">(*columns)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Select columns from the table, given one or more column names. Returns an
instance of <code>Table</code> with the results.  Raises ArgumentError if
any column is not valid.</p>

<h4 id="method-i-select-label-Attributes">Attributes<span><a href="#method-i-select-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>columns</code>
<dd>
<p>Variable <code>String</code> arguments to identify the columns to select</p>
</dd></dl>

<h4 id="method-i-select-label-Examples">Examples<span><a href="#method-i-select-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.select(&quot;City&quot;, &quot;State&quot;)  # returns a Table of &quot;City&quot; and &quot;State&quot; columns
cities.select(cities.headers)  # returns a new Table that is a duplicate of cities</pre>
          
          

          
          <div class="method-source-code" id="select-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 355</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">select</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">columns</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name(s)&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">columns</span>
  <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">flatten!</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
  <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">c</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">result</span> = []
  <span class="ruby-identifier">result_headers</span> = []
  <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">result_headers</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">col</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span> }
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">result_headers</span>
  <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">this_row</span> = []
    <span class="ruby-identifier">result_headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">this_row</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>]
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">this_row</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result_headers</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>() <span class="ruby-operator">:</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>)
<span class="ruby-keyword">end</span></pre>
          </div><!-- select-source -->
          
        </div>

        
        <div class="aliases">
          Also aliased as: <a href="Table.html#method-i-get_columns">get_columns</a>
        </div>
        

        
      </div><!-- select-method -->

    
      <div id="method-i-size" class="method-detail method-alias">
        
        <div class="method-heading">
          <span class="method-name">size</span><span
            class="method-args">(colname=nil, value=nil)</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
        </div>

        

        
        <div class="aliases">
          Alias for: <a href="Table.html#method-i-count">count</a>
        </div>
        
      </div><!-- size-method -->

    
      <div id="method-i-sort" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">sort</span><span
            class="method-args">(column=nil, &block)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Sort the table based on given column. Uses precedence as defined in the 
column. By default will sort by the value in the first column.</p>

<h4 id="method-i-sort-label-Attributes">Attributes<span><a href="#method-i-sort-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>args</code>
<dd>
<p>OPTIONAL <code>String</code> to identify the column on which to sort</p>
</dd></dl>

<h4 id="method-i-sort-label-Options">Options<span><a href="#method-i-sort-label-Options">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre class="ruby"><span class="ruby-identifier">datatype</span> =<span class="ruby-operator">&gt;</span> :<span class="ruby-constant">Fixnum</span>
<span class="ruby-identifier">datatype</span> =<span class="ruby-operator">&gt;</span> :<span class="ruby-constant">Float</span>
<span class="ruby-identifier">datatype</span> =<span class="ruby-operator">&gt;</span> :<span class="ruby-constant">Date</span>
</pre>

<h4 id="method-i-sort-label-Examples">Examples<span><a href="#method-i-sort-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre class="ruby"><span class="ruby-identifier">cities</span>.<span class="ruby-identifier">sort</span>(<span class="ruby-string">&quot;State&quot;</span>)  <span class="ruby-comment"># Re-orders the cities table based on State name</span>
<span class="ruby-identifier">cities</span>.<span class="ruby-identifier">sort</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>,<span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">b</span><span class="ruby-operator">&lt;=&gt;</span><span class="ruby-identifier">a</span> }  <span class="ruby-comment"># Reverse the order of the cities table</span>
<span class="ruby-identifier">cities</span>.<span class="ruby-identifier">sort</span>(<span class="ruby-string">&quot;State&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>,<span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">b</span><span class="ruby-operator">&lt;=&gt;</span><span class="ruby-identifier">a</span> }  <span class="ruby-comment"># Sort by State in reverse alpha order</span>
</pre>
          
          

          
          <div class="method-source-code" id="sort-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 540</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">sort</span>(<span class="ruby-identifier">column</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">col_index</span> = <span class="ruby-value">0</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">String</span>
    <span class="ruby-identifier">col_index</span> = <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">column</span>)
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Fixnum</span>
    <span class="ruby-identifier">col_index</span> = <span class="ruby-identifier">column</span> 
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># return empty Table if empty</span>
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">empty?</span> 
    <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>() 
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">neworder</span> = []
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">neworder</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">OrderedRow</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">row</span>,<span class="ruby-identifier">col_index</span>) }

  <span class="ruby-identifier">result</span> = [<span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">shift</span>.<span class="ruby-identifier">data</span>] <span class="ruby-comment"># take off headers</span>
  <span class="ruby-identifier">block_given?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">sort!</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">sort!</span>
  <span class="ruby-identifier">neworder</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>.<span class="ruby-identifier">data</span> }

  <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>)
<span class="ruby-keyword">end</span></pre>
          </div><!-- sort-source -->
          
        </div>

        
        <div class="aliases">
          Also aliased as: <a href="Table.html#method-i-sort-21">sort!</a>
        </div>
        

        
      </div><!-- sort-method -->

    
      <div id="method-i-sort-21" class="method-detail method-alias">
        
        <div class="method-heading">
          <span class="method-name">sort!</span><span
            class="method-args">(column=nil, &block)</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
        </div>

        

        
        <div class="aliases">
          Alias for: <a href="Table.html#method-i-sort">sort</a>
        </div>
        
      </div><!-- sort-21-method -->

    
      <div id="method-i-sub" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">sub</span><span
            class="method-args">(colname, re, replace)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Given a field/column, and a regular expression to match against, and a
replacement string, update the table such that it substitutes the column
data with the replacement string. Returns <code>nil</code> if the column is
not found.</p>

<h4 id="method-i-sub-label-Attributes">Attributes<span><a href="#method-i-sub-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to join on</p>
</dd><dt><code>re</code>
<dd>
<p><code>Regexp</code> to match the value in the selected column</p>
</dd><dt><code>replace</code>
<dd>
<p><code>String</code> to specify the replacement text for the given
<code>Regexp</code></p>
</dd></dl>

<h4 id="method-i-sub-label-Examples">Examples<span><a href="#method-i-sub-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.sub(&quot;Population&quot;, /(.*?),(.*?)/, &#39;\1\2&#39;)  # eliminate commas
capitals.sub(&quot;State&quot;, /NY/, &quot;New York&quot;)  # replace acronym with full name</pre>
          
          

          
          <div class="method-source-code" id="sub-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 470</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">sub</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">re</span>, <span class="ruby-identifier">replace</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;No regular expression to match against&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">re</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;No replacement string specified&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">replace</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
  
  <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">item</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">item</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-identifier">re</span>, <span class="ruby-identifier">replace</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- sub-source -->
          
        </div>

        
        <div class="aliases">
          Also aliased as: <a href="Table.html#method-i-sub-21">sub!</a>
        </div>
        

        
      </div><!-- sub-method -->

    
      <div id="method-i-sub-21" class="method-detail method-alias">
        
        <div class="method-heading">
          <span class="method-name">sub!</span><span
            class="method-args">(colname, re, replace)</span>
          
        </div>
        

        <div class="method-description">
          
          
          
          

          
        </div>

        

        
        <div class="aliases">
          Alias for: <a href="Table.html#method-i-sub">sub</a>
        </div>
        
      </div><!-- sub-21-method -->

    
      <div id="method-i-tally" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">tally</span><span
            class="method-args">(colname)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Count instances in a particular field/column and return a
<code>Table</code> of the results. Raises ArgumentError if the column is
not found.</p>

<h4 id="method-i-tally-label-Attributes">Attributes<span><a href="#method-i-tally-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to tally</p>
</dd></dl>

<h4 id="method-i-tally-label-Examples">Examples<span><a href="#method-i-tally-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.tally(&quot;State&quot;)  # returns each State in the cities Table with number of occurences</pre>
          
          

          
          <div class="method-source-code" id="tally-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 334</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">tally</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span>  <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)

  <span class="ruby-identifier">result</span> = {}
  <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">colname</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">result</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">val</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">result</span>[<span class="ruby-identifier">val</span>] <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">result</span>[<span class="ruby-identifier">val</span>] = <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>([[<span class="ruby-identifier">colname</span>,<span class="ruby-string">&quot;Count&quot;</span>]] <span class="ruby-operator">+</span> <span class="ruby-identifier">result</span>.<span class="ruby-identifier">to_a</span>)
<span class="ruby-keyword">end</span></pre>
          </div><!-- tally-source -->
          
        </div>

        

        
      </div><!-- tally-method -->

    
      <div id="method-i-to_a" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">to_a</span><span
            class="method-args">()</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Converts a <code>Table</code> object to an array of arrays (each row). The
first entry are the table headers.</p>

<h4 id="method-i-to_a-label-Attributes">Attributes<span><a href="#method-i-to_a-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<p>none</p>
          
          

          
          <div class="method-source-code" id="to_a-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 237</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">to_a</span>
  <span class="ruby-identifier">result</span> = [ <span class="ruby-constant">Array</span>(<span class="ruby-ivar">@headers</span>) ]
  
  <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">items</span> = []
    <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">items</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>]
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">items</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- to_a-source -->
          
        </div>

        

        
      </div><!-- to_a-method -->

    
      <div id="method-i-to_s" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">to_s</span><span
            class="method-args">()</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Converts a <code>Table</code> object to a tab-delimited string.</p>

<h4 id="method-i-to_s-label-Attributes">Attributes<span><a href="#method-i-to_s-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<p>none</p>
          
          

          
          <div class="method-source-code" id="to_s-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 216</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">to_s</span>
  <span class="ruby-identifier">result</span> = <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;\t&quot;</span>) <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;\n&quot;</span>
  
  <span class="ruby-ivar">@table</span>[<span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">first</span>].<span class="ruby-identifier">each_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
    <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">col</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@table</span>[<span class="ruby-identifier">col</span>][<span class="ruby-identifier">index</span>].<span class="ruby-identifier">to_s</span>
      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">col</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">last</span>
        <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;\t&quot;</span>
      <span class="ruby-keyword">else</span>
        <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;\n&quot;</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- to_s-source -->
          
        </div>

        

        
      </div><!-- to_s-method -->

    
      <div id="method-i-top" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">top</span><span
            class="method-args">(colname, num=1)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Returns counts of the most frequent values found in a given column in the
form of a <a href="Table.html">Table</a>.  Raises ArgumentError if the
column is not found.  If no limit is given to the number of values, only
the top value will be returned.</p>

<h4 id="method-i-top-label-Attributes">Attributes<span><a href="#method-i-top-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to count</p>
</dd><dt><code>num</code>
<dd>
<p>OPTIONAL <code>String</code> number of values to return</p>
</dd></dl>

<h4 id="method-i-top-label-Examples">Examples<span><a href="#method-i-top-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.top(&quot;State&quot;)  # returns a Table with the most frequent state in the cities Table
cities.top(&quot;State&quot;, 10)  # returns a Table with the 10 most frequent states in the cities Table</pre>
          
          

          
          <div class="method-source-code" id="top-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 300</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">top</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">num</span>=<span class="ruby-value">1</span>)
  <span class="ruby-identifier">freq</span> = <span class="ruby-identifier">tally</span>(<span class="ruby-identifier">colname</span>).<span class="ruby-identifier">to_a</span>[<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>].<span class="ruby-identifier">sort_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">v</span> }.<span class="ruby-identifier">reverse</span>
  <span class="ruby-keyword">return</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">freq</span>[<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-identifier">num</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>].<span class="ruby-identifier">unshift</span>([<span class="ruby-identifier">colname</span>,<span class="ruby-string">&quot;Count&quot;</span>]))
<span class="ruby-keyword">end</span></pre>
          </div><!-- top-source -->
          
        </div>

        

        
      </div><!-- top-method -->

    
      <div id="method-i-union" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">union</span><span
            class="method-args">(table2, colname, col2name=colname)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Return Array with the union of elements columns in the given tables,
eliminating duplicates. Raises an ArgumentError if a column is not found.</p>

<h4 id="method-i-union-label-Attributes">Attributes<span><a href="#method-i-union-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>table2</code>
<dd>
<p><code>Table</code> to identify the secondary table in the union</p>
</dd><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to union</p>
</dd><dt><code>col2name</code>
<dd>
<p>OPTIONAL <code>String</code> to identify the column in the second table to
union</p>
</dd></dl>

<h4 id="method-i-union-label-Examples">Examples<span><a href="#method-i-union-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.union(capitals, &quot;City&quot;, &quot;Capital&quot;)  # returns Array with all cities in both tables</pre>
          
          

          
          <div class="method-source-code" id="union-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 493</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">union</span>(<span class="ruby-identifier">table2</span>, <span class="ruby-identifier">colname</span>, <span class="ruby-identifier">col2name</span>=<span class="ruby-identifier">colname</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid table!&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Table</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">colname</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid column name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">col2name</span>)

  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">colname</span>) <span class="ruby-operator">|</span> <span class="ruby-identifier">table2</span>.<span class="ruby-identifier">column</span>(<span class="ruby-identifier">col2name</span>)
<span class="ruby-keyword">end</span></pre>
          </div><!-- union-source -->
          
        </div>

        

        
      </div><!-- union-method -->

    
      <div id="method-i-where" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">where</span><span
            class="method-args">(colname, condition=nil)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Given a particular condition for a given column field/column, return a
subtable that matches the condition. If no condition is given, a new
<code>Table</code> is returned with all records. Returns an empty table if
the condition is not met or the column is not found.</p>

<h4 id="method-i-where-label-Attributes">Attributes<span><a href="#method-i-where-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>colname</code>
<dd>
<p><code>String</code> to identify the column to tally</p>
</dd><dt><code>condition</code>
<dd>
<p>OPTIONAL <code>String</code> containing a ruby condition to evaluate</p>
</dd></dl>

<h4 id="method-i-where-label-Examples">Examples<span><a href="#method-i-where-label-Examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre>cities.where(&quot;State&quot;, &quot;==&#39;NY&#39;&quot;)  # returns a Table of cities in New York state 
cities.where(&quot;State&quot;, &quot;=~ /New.*/&quot;)  # returns a Table of cities in states that start with &quot;New&quot;
cities.where(&quot;Population&quot;, &quot;.to_i &gt; 1000000&quot;)  # returns a Table of cities with population over 1 million</pre>
          
          

          
          <div class="method-source-code" id="where-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 393</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">where</span>(<span class="ruby-identifier">colname</span>, <span class="ruby-identifier">condition</span>=<span class="ruby-keyword">nil</span>)
  <span class="ruby-comment"># check arguments</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Invalid Column Name&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@headers</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">colname</span>)

  <span class="ruby-identifier">result</span> = []
  <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@headers</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">condition</span>
      <span class="ruby-identifier">eval</span>(<span class="ruby-string">%q[&quot;#{row[headers.index(colname)]}&quot;]</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;#{condition}&quot;</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">result</span>) <span class="ruby-operator">:</span> <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>()
<span class="ruby-keyword">end</span></pre>
          </div><!-- where-source -->
          
        </div>

        
        <div class="aliases">
          Also aliased as: <a href="Table.html#method-i-get_rows">get_rows</a>
        </div>
        

        
      </div><!-- where-method -->

    
      <div id="method-i-write_file" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">write_file</span><span
            class="method-args">(filename)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Write a representation of the <code>Table</code> object to a file (tab
delimited).</p>

<h4 id="method-i-write_file-label-Attributes">Attributes<span><a href="#method-i-write_file-label-Attributes">&para;</a> <a href="#documentation">&uarr;</a></span></h4>
<dl class="rdoc-list note-list"><dt><code>filename</code>
<dd>
<p><code>String</code> to identify the name of the file to write</p>
</dd></dl>
          
          

          
          <div class="method-source-code" id="write_file-source">
            <pre><span class="ruby-comment"># File lib/tablestakes.rb, line 568</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">write_file</span>(<span class="ruby-identifier">filename</span>)
  <span class="ruby-identifier">file</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-string">&quot;w&quot;</span>)
  <span class="ruby-identifier">file</span>.<span class="ruby-identifier">print</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">to_s</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- write_file-source -->
          
        </div>

        

        
      </div><!-- write_file-method -->

    
    </section><!-- public-instance-method-details -->
  
  </section><!-- 5Buntitled-5D -->

</div><!-- documentation -->


<footer id="validator-badges">
  <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
  <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 4.0.0.
  <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
</footer>

